using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Microsoft.Win32;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace OMR2 {
	static class Program {
		private static void Main() {
		
			int millisecondsTimeout=0;
			int timeout=0;
			List<string> list=new List<string>(50);
			List<Sample> list2=new List<Sample>(50);
			Stopwatch stopwatch=Stopwatch.StartNew();
			try
			{
				using(RegistryKey key=Registry.LocalMachine.OpenSubKey(@"Software\OMR"))
				{
					string outputPath=key.GetValue("LogFolderPath").ToString();
					timeout=(int)key.GetValue("Timeout");
					millisecondsTimeout=(int)key.GetValue("Sleep");
					foreach(string str2 in key.GetSubKeyNames())
					{
						if(!str2.StartsWith("-"))
						{
							using(RegistryKey key2=key.OpenSubKey(str2))
							{
								string str3=key2.GetValue(null) as string;
								if(!string.IsNullOrEmpty(str3))
								{
									foreach(string str4 in key2.GetValueNames())
									{
										if(!str4.StartsWith("-")&&(str4!=""))
										{
											string[] strArray=key2.GetValue(str4) as string[];
											if((strArray!=null)&&(strArray.Length>0))
											{
												string query=string.Join(Environment.NewLine,strArray);
												Sample item=new Sample(str2,str4,query,str3,outputPath,timeout);
												list2.Add(item);
											}
										}
									}
								}
							}
						}
					}
				}
				//StringBuilder builder=new StringBuilder();
				//builder.AppendLine("declare @chkid int");
				foreach(Sample sample in list2)
				{
					list.Add(sample.Log());
					//builder.Append(sample.SQL);
					if(millisecondsTimeout>0)
					{
						Thread.Sleep(millisecondsTimeout);
					}
				}
				//new DAL().NonQry(builder.ToString(),new string[0]);
			}
			catch(Exception exception)
			{
				EventLog.WriteEntry("OMR",exception.ToString(),EventLogEntryType.Warning);
			}
			finally
			{
				EventLog.WriteEntry("OMR",string.Format("Completed in: {0:#.000} sec.{1}{2}",stopwatch.Elapsed.TotalSeconds,Environment.NewLine,string.Join(Environment.NewLine,list.ToArray())));
			}
		}
	}

	public class DAL:IDisposable {
		// Fields
		private SqlConnection con;
		private int timeout;
		// Methods
		public DAL()
			: this("server=(local);database=master;Integrated Security=SSPI;") {
		}
		public DAL(string cs) {
			this.timeout=0;
			this.con=new SqlConnection(cs);
		}
		private void con_InfoMessage(object sender,SqlInfoMessageEventArgs e) {
			Console.WriteLine(e);
		}
		public void Dispose() {
			this.con.Dispose();
		}
		public int NonQry(string sqlForm,params string[] args) {
			for(int i=0;i<args.Length;i++)
			{
				if(args[i].Contains("'"))
				{
					args[i]=args[i].Replace("'","''");
				}
			}
			string cmdText=sqlForm;
			if(args.Length>0)
			{
				cmdText=string.Format(sqlForm,(object[])args);
			}
			int num2=0;
			using(SqlCommand command=new SqlCommand(cmdText,this.con))
			{
				command.CommandTimeout=this.timeout;
				this.con.InfoMessage+=new SqlInfoMessageEventHandler(this.con_InfoMessage);
				this.con.Open();
				num2=command.ExecuteNonQuery();
				this.con.Close();
			}
			return num2;
		}
		public DataTable Query(string sqlForm,params string[] args) {
			string cmdText=string.Format(sqlForm,(object[])args);
			DataTable table=new DataTable();
			using(SqlCommand command=new SqlCommand(cmdText,this.con))
			{
				command.CommandTimeout=this.timeout;
				this.con.InfoMessage+=new SqlInfoMessageEventHandler(this.con_InfoMessage);
				this.con.Open();
				table.Load(command.ExecuteReader());
				this.con.Close();
			}
			return table;
		}
		public object Scalar(string sqlForm,params string[] args) {
			object obj2;
			using(SqlCommand command=new SqlCommand(string.Format(sqlForm,(object[])args),this.con))
			{
				command.CommandTimeout=this.timeout;
				this.con.InfoMessage+=new SqlInfoMessageEventHandler(this.con_InfoMessage);
				this.con.Open();
				obj2=command.ExecuteScalar();
				this.con.Close();
			}
			return obj2;
		}
		public DataSet Set(string sqlForm,params string[] args) {
			string selectCommandText=string.Format(sqlForm,(object[])args);
			DataSet dataSet=new DataSet();
			using(SqlDataAdapter adapter=new SqlDataAdapter(selectCommandText,this.con))
			{
				adapter.Fill(dataSet);
			}
			return dataSet;
		}
	}
}